package txtpack.Work;

import java.io.*;

public class Exam01 {
    //使用字节输出流将一组整数写入到文件中，并使用字节输入流读取这些整数并计算它们的总和。
    public static void main(String[] args) {
        File file =new File("E:\\IO流练习\\InoutStream\\002.txt");
        int[] nums = new int[]{5,4,9,2,6,7,3,2};
        System.out.println(getFileSum(file, nums));
    }
    private static int  getFileSum(File file, int[] nums){
        int sum = 0;
        try (FileInputStream in = new FileInputStream(file);
             FileOutputStream out = new FileOutputStream(file,true);){
            String str = "";
            for (int i : nums) {
               str += String.valueOf(i) + ",";
            }
            str = str.substring(0,str.length() - 1);
            byte[] bytes = str.getBytes();
            out.write(bytes);
            byte[] bytes1 = in.readAllBytes();
            String num = new String(bytes1);
            String[] spilt = num.split(",");
            for (int i = 0; i < spilt.length; i++) {
                sum += Integer.parseInt(spilt[i]);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return sum;
    }
}
